﻿using System.Collections.Generic;

namespace Sidvall.Data
{
    public class ServiceDataRowCollectionItem<TEntity> : IDataRowProvider, IDataRowProvider<TEntity>, Sidvall.Data.ITotalRowCount, IListContextProvider
        where TEntity : ServiceDataRowBase
    {
        #region Public Members

        #region TotalRowCount

        public long? TotalRowCount
        {
            get
            {
                return this.Items.TotalRowCount;
            }
            set
            {
                this.Items.TotalRowCount = value;
            }
        }

        #endregion
        #region DeletedItemKeys

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public List<string> DeletedItemKeys
        {
            get
            {
                return this.Items.DeletedItemKeys;
            }
            set
            {
                this.Items.DeletedItemKeys = value;
            }
        }

        #endregion
        #region Items

        private ServiceDataRowCollection<TEntity> _Items;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ServiceDataRowCollection<TEntity> Items
        {
            get
            {
                if (_Items == null)
                    _Items = new ServiceDataRowCollection<TEntity>();
                return _Items;
            }
            set
            {
                _Items = value;
            }
        }

        #endregion

        #region Add

        public void Add(TEntity item)
        {
            this.Items.Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<TEntity> items)
        {
            this.Items.AddRange(items);
        }

        #endregion
        #region Remove

        public bool Remove(TEntity item)
        {
            return this.Items.Remove(item);
        }

        #endregion
        #region RemoveAt

        public void RemoveAt(int index)
        {
            this.Items.RemoveAt(index);
        }

        #endregion
        #region SetDataRowState

        public void SetDataRowState(Sidvall.Data.DataRowState dataRowState)
        {
            foreach (var item in this.Items)
            {
                if (item.DataRowContext == null)
                    item.DataRowContext = new Sidvall.Data.DataRowContext(dataRowState);
                else
                    item.DataRowContext.DataRowState = dataRowState;
            }
        }

        #endregion

        #region Interface Members

        System.Collections.IEnumerable IDataRowProvider.DataItems
        {
            get
            {
                return this.Items;
            }
        }
        IEnumerable<TEntity> IDataRowProvider<TEntity>.DataItems
        {
            get
            {
                return this.Items;
            }
        }
        long ITotalRowCount.TotalRowCount
        {
            get
            {
                if (this.TotalRowCount != null)
                    return this.TotalRowCount.Value;
                return this.Items.Count;
            }
            set
            {
                this.TotalRowCount = value;
            }
        }
        IEnumerable<string> IListContextProvider.DeletedItemKeys
        {
            get
            {
                return this.DeletedItemKeys;
            }
            set
            {
                this.DeletedItemKeys = new List<string>(value);
            }
        }

        #endregion

        #endregion
        #region Constructors

        public ServiceDataRowCollectionItem()
        {
        }
        public ServiceDataRowCollectionItem(IEnumerable<TEntity> items)
        {
            if (items == null)
                return;
            this.Items = new ServiceDataRowCollection<TEntity>(items);
        }

        #endregion
    }
}
